﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web.Script.Serialization;
using System.Xml.Serialization;

namespace Toybox.NicoSharp.Utilities
{
	using Logger;

	/// <summary>
	/// Deserialize Utility
	/// </summary>
	public static class Deserializer
	{

		#region [Static]

		static Deserializer()
		{
			_javaScriptSerializer = new JavaScriptSerializer();
		}

		private static JavaScriptSerializer _javaScriptSerializer;

		#endregion [Static]


		#region Xml

		/// <summary>
		/// XmlをDeserializeする
		/// </summary>
		/// <param name="xml"></param>
		/// <param name="type"></param>
		/// <returns></returns>
		public static object XmlDeserializer(string xml, Type type)
		{
			try
			{
				using (var sr = new StringReader(xml))
				{
					var xs = new XmlSerializer(type);
					return xs.Deserialize(sr);
				}
			}
			catch (Exception ex)
			{
				//IO.Log.Write(e, xml);
				throw ex;
			}
		}

		/// <summary>
		/// XmlをDeserializeする
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="xml"></param>
		/// <returns></returns>
		public static T XmlDeserializer<T>(string xml)
		{
			return (T)XmlDeserializer(xml, typeof(T));
		}

		#endregion Xml

		#region Json

		/// <summary>
		/// JsonをDeserializeする
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="json"></param>
		/// <returns></returns>
		public static T JsonDeserializer<T>(string json)
		{
			//return (T)JsonDCDeserializer(json, typeof(T));
			return (T)JsonJSSDeserializer(json, typeof(T));
		}

		/// <summary>
		/// JsonをDeserializeする(DataContract)
		/// </summary>
		/// <param name="json"></param>
		/// <param name="type"></param>
		/// <returns></returns>
		public static object JsonDCDeserializer(string json, Type type)
		{
			try
			{
				byte[] bytes = Encoding.UTF8.GetBytes(json);
				Stream strm = new MemoryStream(bytes);
				var js = new DataContractJsonSerializer(type);
				return js.ReadObject(strm);
			}
			catch (Exception ex)
			{
				TraceLogger.WriteLine(ex.ToString());
				return null;
			}
		}

		/// <summary>
		/// JsonをDeserializeする(JavaScriptSerializer)
		/// </summary>
		/// <param name="json"></param>
		/// <param name="type"></param>
		/// <returns></returns>
		public static object JsonJSSDeserializer(string json, Type type)
		{
			try
			{
				return _javaScriptSerializer.Deserialize(json, type);
			}
			catch (Exception ex)
			{
				TraceLogger.WriteLine(ex.ToString());
				return null;
			}
		}

		#endregion Json

		/// <summary>
		/// &,=で構成されたクエリをDeserializeする
		/// </summary>
		/// <param name="html"></param>
		/// <returns></returns>
		public static Dictionary<string, string> AndEqualPairDeserializer(string html)
		{
			string[] pairs = html.Split('&');
			var result = new Dictionary<string, string>();

			foreach (var pair in pairs)
			{
				var element = pair.Split('=');
				result.Add(element[0], element[1]);
			}

			return result;
		}

	}
}
